package com.eoner.uikit.edits;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.eoner.baselibrary.utils.Utils;
import com.eoner.uikit.R;

import java.util.Timer;
import java.util.TimerTask;


public class CodeEditView extends LinearLayout implements TextWatcher, EditText.OnKeyListener {

    MyEditView code0, code1, code2, code3;
//            code4, code5;

    OnCodeFinishListener onCodeFinishListener;
    OnCodeUnFinishListener onCodeUnFinishListener;

    public OnCodeFinishListener getOnCodeFinishListener() {
        return onCodeFinishListener;
    }

    public void setOnCodeFinishListener(OnCodeFinishListener onCodeFinishListener) {
        this.onCodeFinishListener = onCodeFinishListener;
    }

    public void setOnCodeUnFinishListener(OnCodeUnFinishListener onCodeUnFinishListener) {
        this.onCodeUnFinishListener = onCodeUnFinishListener;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL) {
//            if (TextUtils.isEmpty(code5.getText().toString().trim())) {
//                showSoftInput(code4);
//            }
//            if (TextUtils.isEmpty(code4.getText().toString().trim())) {
//                showSoftInput(code3);
//            }
            if (TextUtils.isEmpty(code3.getText().toString().trim())) {
                showSoftInput(code2);
            }
            if (TextUtils.isEmpty(code2.getText().toString().trim())) {
                showSoftInput(code1);
            }
            if (TextUtils.isEmpty(code1.getText().toString().trim())) {
                showSoftInput(code0);
            }
        }
        return false;
    }

    public interface OnCodeFinishListener {
        void onFinish(String code);
    }

    public interface OnCodeUnFinishListener {
        void onUnFinish();
    }

    public CodeEditView(Context context) {
        this(context, null);
    }

    public CodeEditView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CodeEditView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_code_edit, this);
        initView();
        addListener();
    }

    private void addListener() {
        code0.addTextChangedListener(this);
        code1.addTextChangedListener(this);
        code2.addTextChangedListener(this);
        code3.addTextChangedListener(this);
//        code4.addTextChangedListener(this);
//        code5.addTextChangedListener(this);
        code0.setOnKeyListener(this);
        code1.setOnKeyListener(this);
        code2.setOnKeyListener(this);
        code3.setOnKeyListener(this);
//        code4.setOnKeyListener(this);
//        code5.setOnKeyListener(this);

        Utils.setDin(code0, getContext());
        Utils.setDin(code1, getContext());
        Utils.setDin(code2, getContext());
        Utils.setDin(code3, getContext());

    }

    private void initView() {
        code0 = findViewById(R.id.code0);
        code1 = findViewById(R.id.code1);
        code2 = findViewById(R.id.code2);
        code3 = findViewById(R.id.code3);
//        code4 = findViewById(R.id.code4);
//        code5 = findViewById(R.id.code5);

        code0.setOnClipSuccessListener(this::copyTxt);
        code1.setOnClipSuccessListener(this::copyTxt);
        code2.setOnClipSuccessListener(this::copyTxt);
        code3.setOnClipSuccessListener(this::copyTxt);
//        code4.setOnClipSuccessListener(this::copyTxt);
//        code5.setOnClipSuccessListener(this::copyTxt);
    }

    private void copyTxt(String clipTxt) {
        code0.setText(clipTxt.substring(0, 1));
        code1.setText(clipTxt.substring(1, 2));
        code2.setText(clipTxt.substring(2, 3));
        code3.setText(clipTxt.substring(3, 4));
//        code4.setText(clipTxt.substring(4, 5));
//        code5.setText(clipTxt.substring(5, 6));
        code0.setSelection(1);
        code1.setSelection(1);
        code2.setSelection(1);
        code3.setSelection(1);
//        code4.setSelection(1);
//        code5.setSelection(1);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        String code = code0.getText().toString().trim() + code1.getText().toString().trim() + code2.getText().toString().trim() +
                code3.getText().toString().trim();
//        code4.getText().toString().trim() + code5.getText().toString().trim()

        if (!TextUtils.isEmpty(code) && code.length() == 4 && onCodeFinishListener != null) {
            onCodeFinishListener.onFinish(code);
        } else {
            if (onCodeUnFinishListener != null) {
                onCodeUnFinishListener.onUnFinish();
            }
        }
        if (TextUtils.isEmpty(code0.getText().toString().trim())) {
            showSoftInput(code0);
        } else if (TextUtils.isEmpty(code1.getText().toString().trim())) {
            showSoftInput(code1);
        } else if (TextUtils.isEmpty(code2.getText().toString().trim())) {
            showSoftInput(code2);
        } else if (TextUtils.isEmpty(code3.getText().toString().trim())) {
            showSoftInput(code3);
        }

        if (code3.length() > 0) {
            code0.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code1.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code2.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code3.setBackgroundResource(R.drawable.edit_code_bkg_sel);
        } else if (code2.length() > 0 && code3.length() <= 0) {
            code0.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code1.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code2.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code3.setBackgroundResource(R.drawable.edit_code_bkg);
        } else if (code1.length() > 0 && code2.length() <= 0) {
            code0.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code1.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code2.setBackgroundResource(R.drawable.edit_code_bkg);
            code3.setBackgroundResource(R.drawable.edit_code_bkg);
        } else if (code0.length() > 0 && code1.length() <= 0) {
            code0.setBackgroundResource(R.drawable.edit_code_bkg_sel);
            code1.setBackgroundResource(R.drawable.edit_code_bkg);
            code2.setBackgroundResource(R.drawable.edit_code_bkg);
            code3.setBackgroundResource(R.drawable.edit_code_bkg);
        } else if (code0.length() <= 0) {
            code0.setBackgroundResource(R.drawable.edit_code_bkg);
            code1.setBackgroundResource(R.drawable.edit_code_bkg);
            code2.setBackgroundResource(R.drawable.edit_code_bkg);
            code3.setBackgroundResource(R.drawable.edit_code_bkg);
        }
//
//        else if (TextUtils.isEmpty(code4.getText().toString().trim())) {
//            showSoftInput(code4);
//        } else if (TextUtils.isEmpty(code5.getText().toString().trim())) {
//            showSoftInput(code5);
//        }
    }

    public void showSoftInput(EditText editText) {
        if (editText == null) {
            editText = code0;
        }
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();

        //延迟弹出软键盘
        Timer timer = new Timer();
        EditText finalEditText = editText;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) finalEditText.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(finalEditText, 0);
            }
        }, 300);
    }

    public void hideSoftInput(EditText editText) {
        if (editText == null) {
            editText = code3;
        }
        InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager.isActive()) {
            inputManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    public String getCodeText() {
        return code0.getText().toString().trim() +
                code1.getText().toString().trim() +
                code2.getText().toString().trim() +
                code3.getText().toString().trim();
//                code4.getText().toString().trim() +
//                code5.getText().toString().trim();
    }

    public void clear() {
        code0.setText("");
        code1.setText("");
        code2.setText("");
        code3.setText("");
//        code4.setText("");
//        code5.setText("");
    }
}


